import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';

class Wl extends StatefulWidget {
  const Wl({super.key});

  @override
  State<Wl> createState() => _WlState();
}

class _WlState extends State<Wl> {
  final dio = Dio();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: GestureDetector(
              onTap: () async {
                // final res = await dio.get("https://api.github.com/events");
                // print(res)
                final Url = Uri.parse('https://api.github.com/events');
                final res = await http.get(Url);
                // print(res.body);
                final resMap = json.decode(res.body);
                // resMap.forEach((item) => GitEvent(item, resMap));
                // print(resMap.runtimeType);
                // print(GitEvent(resMap, 2));
                for (int i = 0; i <= 10; i++) {
                  print(GitEvent(resMap, i));
                }
              },
              child: Container(
                color: Colors.blue,
              ),
            ),
          ),
          Expanded(
            child: GestureDetector(
              onTap: () async {
                final Url = Uri.parse('https://api.github.com/events');
                HttpClient client = HttpClient();
                HttpClientRequest request = await client.getUrl(Url);
                HttpClientResponse response = await request.close();
                var result = await response.transform(utf8.decoder).join();
                final resMap = json.decode(result);
                // print(resMap);
                print(resMap[0]["repo"]["id"]);
                client.close();
              },
              child: Container(
                color: Colors.red,
              ),
            ),
          )
        ],
      ),
    );
  }
}

// class GitEvent {
//   final String id;
//   final String username;
//
//   GitEvent(resMap) {}
// }

GitEvent(resMap, item) {
  final String id;

  id = resMap[item]["repo"]["name"];
  return id;
}
